#pragma once

#include <iostream>
#include <string>
#include <vector>

#include <ctemplate/template.h>
#include <iomanip>

#include "main_server_model.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";
    class view
    {
    public:
        view()
        {
        }
        ~view()
        {
        }

    public:
        // 首页渲染
        void indexExpandHtml(const std::unordered_map<std::string, std::vector<struct book>> &hotbooks, const std::vector<struct book> &newbooks, std::string *html)
        {
            const std::vector<std::pair<std::string, std::string>> twoTypes = ns_model::twoTypes;
            // 1. 形成路径
            std::string src_html = "./wwwroot/index.html";

            // 2. 形成数字字典
            ctemplate::TemplateDictionary root("index");
            for (const auto &twoType : twoTypes)
            {
                std::vector<struct book> books;
                std::string key = twoType.first + twoType.second;
                auto it = hotbooks.find(key);
                if (it != hotbooks.end())
                {
                    books = it->second;
                }
                std::string templateName = ns_util::stringUtil::getBookTypeReverse(twoType.first) + ns_util::stringUtil::getBookTypeReverse(twoType.second);
                for (const auto &b : books)
                {
                    ctemplate::TemplateDictionary *sub = root.AddSectionDictionary(templateName);

                    sub->SetValue("id", b._book_id);
                    sub->SetValue("type", b._book_type);
                    sub->SetValue("type_href", ns_util::stringUtil::getBookTypeReverse(b._book_type));
                    sub->SetValue("title", b._book_title);
                }
            }

            for (const auto &b : newbooks)
            {
                if (&b == &newbooks.back())
                {
                    break;
                }
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("newbook_list");

                sub->SetValue("id", b._book_id);
                sub->SetValue("author", b._book_author);
                sub->SetValue("title", b._book_title);
                sub->SetValue("introduction", b._book_introduction);
            }

            struct book hotbook = hotbooks.find("玄幻奇幻")->second[0];
            ctemplate::TemplateDictionary *hot = root.AddSectionDictionary("hotbook");
            hot->SetValue("id", hotbook._book_id);
            hot->SetValue("title", hotbook._book_title);
            hot->SetValue("introduction", hotbook._book_introduction);

            struct book newbook = newbooks[6];
            ctemplate::TemplateDictionary *anew = root.AddSectionDictionary("newbook");
            anew->SetValue("id", newbook._book_id);
            anew->SetValue("title", newbook._book_title);
            anew->SetValue("introduction", newbook._book_introduction);

            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 完成渲染功能
            tpl->Expand(html, &root);
        }

        // 全部作品界面渲染
        void allExpandHtml(const std::vector<struct book> &books, std::string page, std::string *html, const std::string model)
        {
            // 小说id 小说类型 小说名

            // 1. 形成路径
            std::string src_html = template_path + "all_books.html";

            // 2. 形成数字字典
            ctemplate::TemplateDictionary root("all_books");
            for (const auto &b : books)
            {
                // std::string _book_id; // 小说id值 -- 唯一标识
                // std::string _book_type; // 小说类型
                // std::string _book_status; // 小说状态
                // std::string _book_title; // 小说名
                // std::string _book_author; // 小说作者
                // std::string _img_url; // 小说图片
                // std::string _book_introduction; // 小说简介
                // std::string _total_words; // 小说总字数
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("book_list");
                sub->SetValue("id", b._book_id);
                sub->SetValue("type", b._book_type);
                sub->SetValue("status", b._book_status);
                sub->SetValue("title", b._book_title);
                sub->SetValue("author", b._book_author);
                sub->SetValue("introduction", b._book_introduction);

                std::ostringstream oss;
                oss << std::fixed << std::setprecision(2) << atof(b._total_words.c_str()) / 10000;
                std::string format_value = oss.str();
                sub->SetValue("total", format_value);
            }

            int page_number = atoi(page.c_str());
            int range = page_number - 2 <= 0 ? 1 : page_number - 2;
            for (int i = range; i < range + 5; i++)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("number_list");
                if (model == "all")
                {
                    sub->SetValue("href", "http://101.42.49.12:8080/books/all/page/" + std::to_string(i));
                }
                else
                {
                    sub->SetValue("href", "http://101.42.49.12:8080/books/type/" + model + "/page/" + std::to_string(i));
                }
                sub->SetValue("page", std::to_string(i));
            }
            root.SetValue("pagenow", std::to_string(page_number));
            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 完成渲染功能
            tpl->Expand(html, &root);
        }

        // 用户个人界面渲染
        void userExpandHtml(const struct user usr, std::string *html)
        {
            // 1. 形成路径
            std::string src_html = template_path + "user.html";

            // 2. 形成数字字典
            ctemplate::TemplateDictionary root("all_books");
            root.SetValue("username", usr._user_name);
            // 3. 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 完成渲染功能
            tpl->Expand(html, &root);
        }
    };
}